<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>Protocol注意事项 | Protocol基本概念</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../2.html" />
    
    
    <link rel="prev" href="../index.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="1" data-basepath=".." data-revision="1442489822571">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Protocol基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="1" data-path="protocol/README.html">
            
                
                    <a href="../protocol/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         Protocol注意事项
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="2.html">
            
                
                    <a href="../2.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         Protocol类型限制
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="3.html">
            
                
                    <a href="../3.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         代理设计模式
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="foundation/README.html">
            
                
                    <a href="../foundation/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         Foundation框架介绍
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="nsstring/README.html">
            
                
                    <a href="../nsstring/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         NSString基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="6.html">
            
                
                    <a href="../6.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         字符串读写
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="7.html">
            
                
                    <a href="../7.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         字符串比较
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="8.html">
            
                
                    <a href="../8.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         字符串搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="9.html">
            
                
                    <a href="../9.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         字符串截取
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="10.html">
            
                
                    <a href="../10.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         字符串替换
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="11.html">
            
                
                    <a href="../11.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         字符串与路径
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="12.html">
            
                
                    <a href="../12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         字符串与基本数据类型转换
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="nsmutablestring/README.html">
            
                
                    <a href="../nsmutablestring/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         NSMutableString基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="14" data-path="15.html">
            
                
                    <a href="../15.html">
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                         NSMutableString常用方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="15" data-path="14.html">
            
                
                    <a href="../14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                         NSMutableString练习
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="16" data-path="nsarray/README.html">
            
                
                    <a href="../nsarray/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>16.</b>
                        
                         NSArray基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="17" data-path="17.html">
            
                
                    <a href="../17.html">
                        <i class="fa fa-check"></i>
                        
                            <b>17.</b>
                        
                         NSArray 遍历
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="18" data-path="18.html">
            
                
                    <a href="../18.html">
                        <i class="fa fa-check"></i>
                        
                            <b>18.</b>
                        
                         NSArray排序
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="19" data-path="19.html">
            
                
                    <a href="../19.html">
                        <i class="fa fa-check"></i>
                        
                            <b>19.</b>
                        
                         NSArray文件读写
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="20" data-path="20.html">
            
                
                    <a href="../20.html">
                        <i class="fa fa-check"></i>
                        
                            <b>20.</b>
                        
                         NSArray 与字符串
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="21" data-path="nsmutablearray/README.html">
            
                
                    <a href="../nsmutablearray/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>21.</b>
                        
                         NSMutableArray基本概念
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Protocol基本概念</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_617">
                    
                        <h1 id="protocol">Protocol其他用法</h1>
<h2 id="">本小节知识点:</h2>
<ol>
<li>【理解】protocol 的使用注意</li>
<li>【理解】基协议</li>
<li>【掌握】@required和@optional关键字</li>
</ol>
<hr>
<h2 id="1protocol-">1.protocol 的使用注意</h2>
<ul>
<li>1)Protocol:就一个用途,用来声明一大堆的方法(不能声明成员变量),不能写实现。</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">SportProtocol</span> &lt;<span class="hljs-title">NSObject</span>&gt;</span>
{
    <span class="hljs-keyword">int</span> _age; <span class="hljs-comment">// 错误写法</span>
}
- (<span class="hljs-keyword">void</span>)playFootball;
- (<span class="hljs-keyword">void</span>)playBasketball;
<span class="hljs-keyword">@end</span>
</code></pre>
<ul>
<li>2)只要父类遵守了某个协议,那么子类也遵守。</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">SportProtocol</span> &lt;<span class="hljs-title">NSObject</span>&gt;</span>

- (<span class="hljs-keyword">void</span>)playFootball;
- (<span class="hljs-keyword">void</span>)playBasketball;
<span class="hljs-keyword">@end</span>
</code></pre>
<pre><code class="lang-objc"><span class="hljs-preprocessor">#import <span class="hljs-title">"SportProtocol.h"</span></span>
<span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">Student</span> : <span class="hljs-title">NSObject</span> &lt;<span class="hljs-title">SportProtocol</span>&gt;</span>
<span class="hljs-keyword">@end</span>

<span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">GoodStudent</span> : <span class="hljs-title">Student</span></span>
<span class="hljs-keyword">@end</span>

<span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">GoodStudent</span></span>
- (<span class="hljs-keyword">void</span>)playFootball
{
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%s"</span>, __func__);
}
- (<span class="hljs-keyword">void</span>)playBasketball
{
    <span class="hljs-built_in">NSLog</span>(<span class="hljs-string">@"%s"</span>, __func__);
}
<span class="hljs-keyword">@end</span>
</code></pre>
<ul>
<li>3)OC不能继承多个类(单继承)但是能够遵守多个协议。继承(:),遵守协议(&lt; &gt;)</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-preprocessor">#import <span class="hljs-title">"SportProtocol.h"</span></span>
<span class="hljs-preprocessor">#import <span class="hljs-title">"StudyProtocol.h"</span></span>

<span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">Student</span> : <span class="hljs-title">NSObject</span> &lt;<span class="hljs-title">SportProtocol</span>, <span class="hljs-title">StudyProtocol</span>&gt;</span>

<span class="hljs-keyword">@end</span>
</code></pre>
<ul>
<li>4)协议可以遵守协议,一个协议遵守了另一个协议,就可以拥有另一份协议中的方法声明</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">A</span></span>
-(<span class="hljs-keyword">void</span>)methodA;
<span class="hljs-keyword">@end</span>

<span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">B</span> &lt;<span class="hljs-title">A</span>&gt;</span>
-(<span class="hljs-keyword">void</span>)methodB;
<span class="hljs-keyword">@end</span>
</code></pre>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">Student</span> : <span class="hljs-title">NSObject</span> &lt;<span class="hljs-title">B</span>&gt;</span>
-(<span class="hljs-keyword">void</span>)methodA; <span class="hljs-comment">// 同时拥有A/B协议中的方法声明</span>
-(<span class="hljs-keyword">void</span>)methodB;
<span class="hljs-keyword">@end</span>
</code></pre>
<hr>
<h2 id="2">2.基协议</h2>
<ul>
<li><p>NSObject是一个基类，最根本最基本的类，任何其他类最终都要继承它</p>
</li>
<li><p>还有名字也叫NSObject的协议，它是一个基协议，最根本最基本的协议</p>
</li>
<li><p>NSObject协议中声明很多最基本的方法</p>
<ul>
<li>description</li>
<li>retain</li>
<li>release</li>
</ul>
</li>
<li><p>建议每个新的协议都要遵守NSObject协议</p>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">SportProtocol</span> &lt;<span class="hljs-title">NSObject</span>&gt; // 基协议</span>

- (<span class="hljs-keyword">void</span>)playFootball;
- (<span class="hljs-keyword">void</span>)playBasketball;
<span class="hljs-keyword">@end</span>
</code></pre>
<hr>
<h2 id="3requiredoptional">3.@required和@optional关键字</h2>
<ul>
<li>协议中有2个关键字可以控制方法是否要实现(默认是@required，在大多数情况下，用途在于程序员之间的交流)<ul>
<li>@required：这个方法必须要实现（若不实现，编译器会发出警告）</li>
<li>@optional：这个方法不一定要实现</li>
</ul>
</li>
</ul>
<pre><code class="lang-objc"><span class="hljs-class"><span class="hljs-keyword">@protocol</span> <span class="hljs-title">SportProtocol</span> &lt;<span class="hljs-title">NSObject</span>&gt;</span>

<span class="hljs-keyword">@required</span> <span class="hljs-comment">// 如果遵守协议的类不实现会报警告</span>
- (<span class="hljs-keyword">void</span>)playFootball;
<span class="hljs-keyword">@optional</span> <span class="hljs-comment">// 如果遵守协议的类不实现不会报警告</span>
- (<span class="hljs-keyword">void</span>)playBasketball;
<span class="hljs-keyword">@end</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../index.html" class="navigation navigation-prev " aria-label="Previous page: Protocol基本概念"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../2.html" class="navigation navigation-next " aria-label="Next page: Protocol类型限制"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
